﻿
#ifndef ___THREAD_SEND_H__
#define ___THREAD_SEND_H__

#include <vector>
#include <list>
#include <pthread.h>
#include <atomic>   //c++11里的原子操作
#include <semaphore.h>  //信号量 
#include "public.h"




//线程池相关类
class CSendThread
{
public:
        
    CSendThread();               
    
    ~CSendThread();                           
public:
    bool Start(int threadNum);
	
    void Stop();

    void AddTask(char* psendbuf);
private:
    void ClearMsgSendQueue(); 
private:
    struct ThreadItemSend   
    {
        pthread_t           _Handle;                         
        CSendThread     	*_pThis;                          
        bool                ifrunning;                      
        ThreadItemSend(CSendThread *pthis):_pThis(pthis),ifrunning(false){}
        ~ThreadItemSend(){}
    };
private:
    
	bool                                                            m_shutdown;    

    int                                                               m_iThreadNum;

    std::vector<ThreadItemSend*>             		m_threadVector;

public:

    static void                                                 *ThreadSendFun(void* threadData);                 //专门用来发送数据的线程
    ssize_t                                                         SendProc(sClient* c, char* buff, ssize_t size);
    
    pthread_mutex_t                                       m_sendMessageQueueMutex;               //发消息队列互斥量 

    sem_t                                                      	    m_semEventSendQueue;                   //处理发消息线程相关的信号量 
	
    std::list<char*>                   							m_MsgSendQueue;                       //发送数据消息队列

    std::atomic<int>                 							m_iSendMsgQueueCount;           //发消息队列大小

    int                               							        m_iDiscardSendPkgCount;                 //丢弃的发送数据包数量
};

#endif
